package cn.tedu.luban.demand.main.repository;

import cn.tedu.luban.demand.main.converter.DemandConverter;
import cn.tedu.luban.demand.main.dao.DemandDAO;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.dos.RequestOrderDO;
import cn.tedu.luban.demand.protocol.param.GrabOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderDBPagerQuery;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DemandRepoImpl implements DemandRepository{

    @Autowired
    private DemandDAO demandDAO;

    @Autowired
    private DemandConverter demandConverter;

    @Override
    public Long getRequestOrderCount(RequestOrderQuery requestOrderQuery) {
        // 分页查询 query转换为dbQuery
        RequestOrderDBPagerQuery dbPagerQuery = demandConverter.toDbPagerQuery(requestOrderQuery);
        // 查询数据库
        return demandDAO.countRequestOrder(dbPagerQuery);
    }

    @Override
    public List<RequestOrderBO> queryRequestOrders(RequestOrderQuery requestOrderQuery) {
        // 分页查询 query转换为dbQuery
        RequestOrderDBPagerQuery dbPagerQuery = demandConverter.toDbPagerQuery(requestOrderQuery);
        // 查询数据库
        List<RequestOrderDO> dos = demandDAO.queryRequestOrders(dbPagerQuery);
        // 转换为业务对象
        return demandConverter.poList2BoList(dos);
    }

    @Override
    public Integer grabOrder(String requestOrderNo) {
        // 抢单: update 表名 set grab_status = 1 where request_order_no = #{} and grab_status = 0;
        // 还单：update 表名 set grab_status = 0 where request_order_no = #{} and grab_status = 1;
        // 动态SQL: update 表名 set grab_status = 条件 where request_order_no = #{} and grab_status = #{};
        RequestOrderDO requestOrderDO = new RequestOrderDO();
        requestOrderDO.setRequestOrderNo(requestOrderNo);
        requestOrderDO.setGrabStatus(0);
        return demandDAO.updateGrabStatusByRequestOrderNo(requestOrderDO);
    }

    @Override
    public Integer grabOrder(GrabOrderParam grabOrderParam) {
        return null;
    }

    @Override
    public RequestOrderBO getRequestOrder(String requestOrderNo) {
        return null;
    }

    @Override
    public Integer returnOrder(GrabOrderParam requestOrderNo) {
        return null;
    }

    @Override
    public Integer returnOrder(String requestOrderNo) {
        RequestOrderDO requestOrderDO = new RequestOrderDO();
        requestOrderDO.setRequestOrderNo(requestOrderNo);
        requestOrderDO.setGrabStatus(1);
        return demandDAO.updateGrabStatusByRequestOrderNo(requestOrderDO);
    }

    @Override
    public RequestOrderBO getRequestOrderGrabed(String no) {
        return null;
    }
}
